﻿using Core.Domain;
using Core.DomainServices;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.DomainServices
{
    public class BodyParser : IBodyParser<HtmlNodeCollection>
    {


        public IList<QuestionAndAnswer> Parser(HtmlNodeCollection tables)
        {
            var items = new List<QuestionAndAnswer>();
            foreach (var table in tables)
            {
                var rows = table.SelectNodes("//tr");
                bool first = false;
                foreach (var row in rows)
                {
                    if (first)
                    {
                        try
                        {

                            var docxx = new HtmlDocument();
                            docxx.LoadHtml(row.InnerHtml);
                            var tds = docxx.DocumentNode.SelectNodes("//td");
                            items.Add(
                                    new QuestionAndAnswer 
                                        {
                                            Question = tds[0].InnerText,
                                            CreatedDate = tds[1].InnerText,
                                            Answer = tds[2].InnerText
                                        }
                                    );

                        }
                        catch { }
                    }
                    first = true;
                }
            }

            return items;
        }
    }
}
